package com.own.component.common.exception;

import com.own.base.BaseResponseCode;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

/**
 * BusinessException
 *
 * @author chenxueli
 * @date 2020/6/15 11:01
 */
@Slf4j
@Getter
public class BusinessException extends RuntimeException {

    /**
     * 异常消息code
     */
    private String code;
    /**
     * 异常消息描述tips
     */
    private String message;
    /**
     * 异常数据
     */
    private Throwable error;

    public BusinessException() {
    }

    public BusinessException(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public BusinessException(String message) {
        this.message = message;
        this.code = "00001";
    }

    public BusinessException(BaseResponseCode responseCode) {
        this.message = responseCode.desc();
        this.code = responseCode.code();
    }

    public BusinessException(BaseResponseCode responseCode, Throwable error) {
        this(responseCode);
        this.error = error;
    }

    public BusinessException(BaseResponseCode responseCode, Object... args) {
        this.message = String.format(responseCode.desc(), args);
        this.code = responseCode.code();
    }

    public BusinessException(String message, Throwable cause) {
        super(message, cause);
    }

    public BusinessException(Throwable cause) {
        super(cause);
    }

    public BusinessException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }

    public BusinessException append(String message, Object... args) {
        this.message = String.format(message, args);
        this.code = "00001";
        return this;
    }

}
